//
//  JYPhotoBrowserAnimation.swift
//  WeiBoDemo
//
//  Created by 万佳阳 on 16/8/8.
//  Copyright © 2016年 JYWan. All rights reserved.
//

import UIKit

protocol AnimationPresentedDelegate {
    func startRect(indexPath : NSIndexPath) -> CGRect
    func endRect(indexPath : NSIndexPath) -> CGRect
    func imageView(indexPath : NSIndexPath) -> UIImageView
}

class JYPhotoBrowserAnimation : NSObject {
    private var isPresented : Bool = false
    
    var indexPath : NSIndexPath?
    var presentedDelegate : AnimationPresentedDelegate?
}

extension JYPhotoBrowserAnimation : UIViewControllerTransitioningDelegate {
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        return self
    }
    
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        return self
    }
}

extension JYPhotoBrowserAnimation : UIViewControllerAnimatedTransitioning {
     func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 1.0
    }
    
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        isPresented ? animationForPresentedView(transitionContext) : animationForDismissedView(transitionContext)
    }
    
    private func animationForPresentedView(transitionContext : UIViewControllerContextTransitioning) {
        guard let indexPath = indexPath, presentedDelegate = presentedDelegate else {
            return
        }
        
        //获取弹出的view
        let presentedView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        
        //将view添加到containerView
        transitionContext.containerView()?.addSubview(presentedView)
        
        //获取执行动画的imageView
        let startFrame = presentedDelegate.startRect(indexPath)
        let imageView = presentedDelegate.imageView(indexPath)
        transitionContext.containerView()?.addSubview(imageView)
        imageView.frame = startFrame
        
        //执行动画
        presentedView.alpha = 0.0
        transitionContext.containerView()?.backgroundColor = UIColor.blackColor()
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
            imageView.frame = presentedDelegate.endRect(indexPath)
            }) { (_) in
                imageView.removeFromSuperview()
                presentedView.alpha = 1.0
                transitionContext.containerView()?.backgroundColor = UIColor.clearColor()
                transitionContext.completeTransition(true)
        }
    }
    
    private func animationForDismissedView(transitionContext : UIViewControllerContextTransitioning) {
        //获取消失的view
        let dismissedView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        
        //执行动画
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { 
            dismissedView.alpha = 0.0
            }) { (_) in
                dismissedView.removeFromSuperview()
                transitionContext.completeTransition(true)
        }
    }
}